<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\OrderGoods;
use app\model\Category;
use app\model\Storagelocation;
use app\model\StoragelocationNumber;
use app\model\RegisterField;

class OrdergoodsController extends Base
{

    public function detail()
    {
        $id = input('get.id', '', 'serach_in');
        $data = OrderGoods::find($id);
        $data['is_storagelocation'] = Category::getisstoragelocation($data['cat_id']);
        if ($data['is_storagelocation']) {
            $sid = $this->userInfo['sid'];
            $data['sid'] = $sid;
            $data['Storagelocationarray'] = Storagelocation::getpagearray($sid);
        }

        return $this->json(['data' => $data]);
    }

    public function upstoragelocation()
    {
        try {
            $post = input('post.', '', 'serach_in');
            if ($post['storagelocation']) {
                $Storagelocationdata =  Storagelocation::find($post['storagelocation']);
                if ($Storagelocationdata) {
                    $storagelocationint = StoragelocationNumber::getstoragelocationint($post['storagelocation']);
                    if (!empty($storagelocationint)) {
                        if ($post['id']) {
                            $result =  OrderGoods::where('id', $post['id'])->update([
                                'storagelocation' => $post['storagelocation'],
                                'storagelocationtext' => $Storagelocationdata->title . $storagelocationint,
                                'storagelocationint' => $storagelocationint
                            ]);
                        }
                    } else {
                        return $this->json(['errno' => 1, 'msg' => '挂点已用完', 'message' => '挂点已用完']);
                    }
                }
            }
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return $this->json(['errno' => 1, 'msg' => $msg, 'message' => $msg, 'data' => $result]);
        }
        return $this->json(['data' => $result]);
    }
    public function custom()
    {
        $ogid = input('post.ogid', '', 'intval');
        $registerfield['fields'] =  json_decode(input('post.registerfield'), true);

        $data = RegisterField::fieldToData($registerfield)['data'];

        OrderGoods::where('id', $ogid)->update(['customtext' => $data['customtext']]);

        $msg = '提交成功';
        return $this->json(['msg' => $msg, 'data' => $data]);
    }
}
